import { Context } from '../../types'
import BaseController from '../base'
import Decorator from '../../utils/decorator'
import { paramsValidater } from '../../utils'

export default class UserController extends BaseController {
  public static async query() {
    return {}
  }

  @Decorator.Post
  public static async create(ctx: Context, params) {
    return {}
  }

  @Decorator.Post
  public static async update(ctx: Context, params) {
    return {}
  }

  @Decorator.Post
  public static async delete(ctx: Context, params) {
    return {}
  }

  /**
   * @description 注册账户
   * @param username
   * @param fp (fromPassword) 指前端公钥加密后的注册密码
   * @param name
   * @param tag
   * @param phone
   * @param email
   * @param avatar
   * @returns
   */
  @Decorator.Post
  public static async register(ctx: Context, params) {
    params.create_time = new Date().toISOString()
    return await paramsValidater(
      ctx,
      async () => {
        return await ctx.service.register(ctx.model, params)
      },
      params,
      [],
      ['username', 'fp', 'name', 'tag', 'phone', 'email'],
    )
  }

  /**
   * @description 用户登录
   * @param username
   * @param fp
   * @returns
   */
  @Decorator.Get
  public static async login(ctx: Context, params) {
    params.create_time = new Date().toISOString()
    return await paramsValidater(
      ctx,
      async () => {
        return await ctx.service.login(ctx, params)
      },
      params,
      [],
      ['username', 'fp'],
    )
  }
}
